#ifndef MEMORY_BLOCK_DEMO_HPP
#define MEMORY_BLOCK_DEMO_HPP

#include <iostream>
#include <memory>

extern "C" {
struct DLL_EXPORT memory_block {
  char *ptr;
  int ptr_size;
};
DLL_EXPORT bool create_memory_block(memory_block **block, int size) {
  if (block) {
    *block = (memory_block *)std::malloc(sizeof(memory_block));
    if (*block) {
      (*block)->ptr = (char *)std::malloc(size);
      (*block)->ptr_size = size;
      for (int i = 0; i < size; i++) {
        (*block)->ptr[i] = (char)(i % 256);
      }
      return true;
    }
  }
  return false;
}
DLL_EXPORT void destroy_memory_block(memory_block **block) {
  if (block && *block) {
    if ((*block)->ptr) {
      std::free((*block)->ptr);
    }
    std::free(*block);
    *block = NULL;
  }
}

typedef void (*callback_from_c_sharp)(int value);

DLL_EXPORT void test_callback(callback_from_c_sharp callback, int value) {
  if (callback) {
    callback(value);
  }
}

void memory_block_demo() {
  memory_block *block = nullptr;
  if (create_memory_block(&block, 100)) {
    destroy_memory_block(&block);
    std::cout << "success" << std::endl;
  } else {
    std::cout << "failed" << std::endl;
  }
}
}

#endif // MEMORY_BLOCK_DEMO_HPP